*WIC co-op eWIC twfe diff in diff analysis
*last modified: 18 April 2025
*last modified by: Kathya Tapia-Schythe
*this do file tabulates twfe and graphs csdid estimated effects of WIC EBT on SNAP and WIC redemptions per county and zip respectively

*-------------------------------------------------------------------------------
*--- Preamble
*-------------------------------------------------------------------------------

clear all
set more off
set rmsg on

set maxvar 120000
set emptycells drop

*ssc install estout, replace

*-------------------------------------------------------------------------------
*--- Directories and Log 
*-------------------------------------------------------------------------------

local data_dir ./data/cleaned
local raw_dir ./data/raw
local out_dir ./analysis/output
local graph_dir ./analysis/output/graphs
local tab_dir ./analysis/output/tables
local log_dir ./documentation/logs
local date: display %tdYY-NN-DD date(c(current_date), "DMY")
di "`date'"
capture log close

log using `log_dir'/tables_figures_voucher`date', replace

*-------------------------------------------------------------------------------
*--- CSDID: Table
*-------------------------------------------------------------------------------

use `out_dir'/voucher_csdid_auth_simple.dta, clear

replace subsample = subinstr(subsample, " redesign", "", 10)

mkmat b se p, matrix(simpletab) rownames(subsample)

estout matrix(simpletab, fmt(3 3 3)) using "`tab_dir'/csdid_voucher.tex", style(tex) label replace

*-------------------------------------------------------------------------------
*--- TWFE: Table
*-------------------------------------------------------------------------------
/* 
foreach out in entry exit auth {
 foreach v of numlist 0 1 {
	
 estimates use `out_dir'/voucher_twfe_`out'`v'_simple.ster
 eststo twfe_`out'`v'_simple

 lincom ebt
 local lb = string(r(lb), "%05.3f") // lower bound
 local ub = string(r(ub), "%05.3f") // upper bound

 estadd local ci "[`lb',`ub']": twfe_`out'`v'_simple
 estadd local fy "Yes": twfe_`out'`v'_simple
 }
}

#delimit ;
esttab twfe_entry0_simple twfe_entry1_simple 
       twfe_exit0_simple  twfe_exit1_simple 
       twfe_auth0_simple  twfe_auth1_simple 
using "`tab_dir'/twfe_voucher.tex",
stats(ci fy N, fmt(%05.3f %-5s %9.0gc)
labels(" " "\\ FY FE" "\midrule \textit{N}")) keep(ebt) label
b(%-9.3f) not noobs nonotes nogaps mlabels(, none) nonumbers style(tex)
fragment replace noline starlevel("*" 0.10 "**" 0.05 "***" 0.01) 
coef(ebt "WIC EBT");
#delimit cr
estimates clear */

*-------------------------------------------------------------------------------
*--- CSDID: Figures
*-------------------------------------------------------------------------------

*ssc install blindschemes, replace 
set scheme plotplainblind
graph set window fontface "Times New Roman"

*a.- Relative Time

use `out_dir'/voucher_csdid_auth_event.dta, clear
drop if missing(se)
encode subsample, gen(voucher)
replace ev_yr=ev_yr+.15 if voucher==1

mkf simple
cwf simple
use `out_dir'/voucher_csdid_auth_simple.dta, clear
summ b if regexm(subsample, "before"), meanonly
local earlyatt = round(r(mean), 0.001)
summ ll if regexm(subsample, "before"), meanonly
local earlyll = round(r(mean), 0.001)
summ ul if regexm(subsample, "before"), meanonly
local earlyul = round(r(mean), 0.001)
summ b if regexm(subsample, "after"), meanonly
local lateatt = round(r(mean), 0.001)
summ ll if regexm(subsample, "after"), meanonly
local latell = round(r(mean), 0.001)
summ ul if regexm(subsample, "after"), meanonly
local lateul = round(r(mean), 0.001)
cwf default

#delimit ; 
twoway rarea avgul avgll ev_yr if ev_yr<0 & voucher==2 & b!=.,  
color(red%25) ||
rarea avgul avgll ev_yr if ev_yr>=0 & voucher==2 & b!=., 
color(red%25) ||
scatter b  ev_yr if voucher==2, mcolor(red%50) msymbol(O) 
msize(medlarge) || 
rcap ul ll ev_yr if voucher==2, msize(vtiny) lcolor(red) ||
line avgb ev_yr if ev_yr<0 & voucher==2 & b!=.,  
lcolor(red) lpattern(solid) ||
line avgb ev_yr if ev_yr>=0 & voucher==2 & b!=., 
lcolor(red) lpattern(solid) ||
rarea avgul avgll ev_yr if ev_yr<0 & voucher==1 & b!=.,  
color(blue%25) ||
rarea avgul avgll ev_yr if ev_yr>=0 & voucher==1 & b!=., 
color(blue%25) ||
scatter b  ev_yr if voucher==1, mcolor(blue%50) msymbol(O) 
msize(medlarge) || 
rcap ul ll ev_yr if voucher==1, msize(vtiny) lcolor(blue) ||
line avgb ev_yr if ev_yr<0 & voucher==1 & b!=.,  
lcolor(blue) lpattern(solid) ||
line avgb ev_yr if ev_yr>=0 & voucher==1 & b!=., 
lcolor(blue) lpattern(solid)
yline(0, lcolor(black) lpattern(dash)) 
xline(0, lcolor(red) lpattern(dot) lwidth(medthick))
ytitle("ATT estimate") xtitle("Event Year")
note("Post-EBT ATT estimate for years before package change is `earlyatt'." "Post-EBT ATT estimate for years after package change is `lateatt'.")
legend(order(1 "Before Package Change" 
                7 "After Package Change") 
        rows(1) position(6));
#delimit cr

graph export "`graph_dir'/voucher_csdid_auth_all.png", replace

*b.- Treatment group

use `out_dir'/voucher_csdid_auth_group.dta, clear
encode subsample, gen(voucher)

#delimit ; 
twoway rarea avgul avgll tmt_yr if voucher==2 & b!=., color(red%25) ||
scatter b  tmt_yr if voucher==2, mcolor(red%50) msymbol(O) 
msize(medlarge) || 
rcap ul ll tmt_yr if voucher==2, msize(vtiny) lcolor(red) ||
line avgb tmt_yr if voucher==2 & b!=., lcolor(red) 
lpattern(solid) ||
rarea avgul avgll tmt_yr if voucher==1 & b!=.,  
color(blue%25) ||
scatter b  tmt_yr if voucher==1, mcolor(blue%50) msymbol(O) 
msize(medlarge) || 
rcap ul ll tmt_yr if voucher==1, msize(vtiny) lcolor(blue) ||
line avgb tmt_yr if voucher==1 & b!=.,  
lcolor(blue) lpattern(solid) 
yline(0, lcolor(black) lpattern(dash)) 
ytitle("ATT estimate") xtitle("FY WIC EBT First Implemented")
legend(order(1 "Before Voucher Redesign" 
                7 "After Voucher Redesign") 
        rows(1) position(6))
xlabel(2006(2)2018);
#delimit cr

graph export "`graph_dir'/voucher_csdid_auth_all_group.png", replace 

*-------------------------------------------------------------------------------
*--- SA: 2x2 Tables
*-------------------------------------------------------------------------------

foreach voucher in group time {
	
 if ("`voucher'"=="group") {
  local lab ebtgroup_before "WIC EBT $\times$ Before Package Change" ///
            ebtgroup_after  "WIC EBT $\times$ After Package Change" 
  local keep1 ebtgroup_before 
  local keep2 ebtgroup_after
 }
 
 if ("`voucher'"=="time") {
  local lab ebttime_before "WIC EBT $\times$ Before Package Change" ///
            ebttime_after  "WIC EBT $\times$ After Package Change"
  local keep1 ebttime_before 
  local keep2 ebttime_after
 }
	
 foreach c in never last {
 	
  local i=1
	
  foreach out in sa_voucher_`voucher'_`c'_all   ///
		 sa_voucher_`voucher'_`c'_chain ///
                 sa_voucher_`voucher'_`c'_indep {
		 		       	
  estimates use `out_dir'/`out'.ster
  estimates store est`i'_`c'
  
  lincom `keep1'-`keep2'
  estadd scalar diff = r(estimate)
  
  local ++i
  
  }
 }
	
 #delimit ;
 esttab est1_never est2_never est3_never
	est1_last  est2_last  est3_last
 using "`tab_dir'/sa_2x2table_tip_`voucher'.tex",
 stats(diff N, fmt(%-9.3f %9.0gc) labels("\\ Difference" "\textit{N}")
 /*star(diff)*/) keep(`keep') label
 b(%-9.3f) se(%-9.3f) not noobs nonotes nogaps nomtitles nonumbers 
 style(tex) fragment replace noline starlevel("*" 0.10 "**" 0.05 "***" 0.01)
 coef(`lab');
 #delimit cr
 estimates clear
 
}


log close
